package com.mb.ext.web.util;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.ObjectMetadata;
import com.mb.ext.rmi.util.AliYunProperties;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.Calendar;
import java.util.Random;

public class OSSUtil {

    public final static String objectUrlTpl = "https://%s/%s";

    /**
     * 上传文件
     * @param objectName
     * @param file
     * @return
     */
    public static String uploadFile(String objectName, MultipartFile file) {
        OSS ossClient = new OSSClientBuilder().build(AliYunProperties.ossEndpoint, AliYunProperties.accessKey, AliYunProperties.accessSecret);
        ObjectMetadata objectMetadata = new ObjectMetadata();
        objectMetadata.setContentLength(file.getSize());
        objectMetadata.setContentDisposition(file.getOriginalFilename());
        try {
            ossClient.putObject(AliYunProperties.ossBucketName, objectName, file.getInputStream(), objectMetadata);
        } catch (IOException e) {
            e.printStackTrace();
            ossClient.shutdown();
            return null;
        }
        ossClient.shutdown();
        return String.format(objectUrlTpl, AliYunProperties.ossBucket, objectName);
    }

    /**
     * 上传字节流
     * @param objectName
     * @param file
     * @param size
     * @param fileName
     * @return
     */
    public static String uploadFileByByte(String objectName, ByteArrayInputStream file, Long size, String fileName) {
        OSS ossClient = new OSSClientBuilder().build(AliYunProperties.ossEndpoint, AliYunProperties.accessKey, AliYunProperties.accessSecret);
        ObjectMetadata objectMetadata = new ObjectMetadata();
        objectMetadata.setContentLength(size);
        objectMetadata.setContentDisposition(fileName);
        ossClient.putObject(AliYunProperties.ossBucketName, objectName, file, objectMetadata);
        ossClient.shutdown();
        return "https://" + AliYunProperties.ossBucket + "/" + objectName;
    }

    public static void deleteFile(String objectName) {
        OSS ossClient = new OSSClientBuilder().build(AliYunProperties.ossEndpoint, AliYunProperties.accessKey, AliYunProperties.accessSecret);
        ossClient.deleteObject(AliYunProperties.ossBucketName, objectName);
        ossClient.shutdown();
    }

    public static void deleteFileByUrl(String file) {
        if (StringUtils.isBlank(file) || file.length() < 10) {
            return;
        }
        int index = file.indexOf('/', 8);
        if (index != -1) {
            deleteFile(file.substring(index + 1));
        }
    }

    public static void deleteFilesByUrl(String files) {
        if (StringUtils.isBlank(files)) {
            return;
        }
        String[] split = files.split("\\|");
        for (String file : split) {
            deleteFileByUrl(file);
        }
    }

    public static String createObjectName(String filename) {
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH) + 1;
        String path = year + "/" + month + "/" + randomStringFixLength(10).toLowerCase() + "/" + filename;
        return path;
    }
    /**
     * 随机字母或数字，固定长度
     */
    public static String randomStringFixLength(int length) {
        return RandomStringUtils.random(length, 0, 0, true, true, null, threadLocalRandom());
    }
    public static Random threadLocalRandom() {
        return java.util.concurrent.ThreadLocalRandom.current();
    }
}
